/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.log.analysis.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import profile.hujia.log.analysis.common.ConstantVariable;
import profile.hujia.log.analysis.common.FileManage;
import profile.hujia.log.analysis.data.LogDataService;

/**
 * class info
 *
 * @author hujia
 * @date 2018/12/17 12:29
 */
@Service
@Slf4j
public class AnalysisServiceImpl implements AnalysisService {

    private final FileManage fileManage;

    private final ReadIntoMemoryService readIntoMemoryService;

    @Value("${hujia.log.saveFile}")
    private String saveFile;

    @Autowired
    public AnalysisServiceImpl(FileManage fileManage, ReadIntoMemoryService readIntoMemoryService) {
        this.fileManage = fileManage;
        this.readIntoMemoryService = readIntoMemoryService;
    }

    public void analysis() throws Exception {
        LogDataService dataService;
        readIntoMemoryService.init();
        while ((dataService = readIntoMemoryService.readIntoMap()) != null) {
            StringBuilder text = dataService.load();
            fileManage.save(saveFile, ConstantVariable.FILE_EXTENSION, text);
        }
        readIntoMemoryService.close();
    }

}
